package com.pbs.ui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.pbs.net.WSF;
import com.pbs.ui.dialog.CreateAccountDialog;
import com.pbs.ui.dialog.Dialog;
import com.pbs.ui.dialog.RestoreAccountDialog;
import com.pbs.ui.dialog.Dialog.Type;

public class AccountManagerScreen extends Activity {

	/** Called when the activity is first created. */
	public static String		US;
	public static String		PW;
	public static int			USERID;
	private static final String	STRING_NAME_USERSETTING	= "USER REFERENCES";
	private SharedPreferences	mShareRefs				= null;
	private Handler						myHandler				= new Handler();

	public static int getUserID() {
		return USERID;
	}

	public static void setUsername(String username) {
		US = username;
	}

	public static void setPassword(String password) {
		PW = password;
	}

	public static String getUsername() {
		return US;
	}

	public static String getPassword() {
		return PW;
	}

	public void ExitClick(View v) {
		System.exit(0);
	}

	public void addSharedPreferences(String key, String value) {
		mShareRefs = getSharedPreferences(STRING_NAME_USERSETTING, Activity.MODE_PRIVATE);
		Editor editor = mShareRefs.edit();
		if (value.trim().equals("")) {
			editor.clear();
		} else {
			editor.putString(key, value);
		}
		editor.commit();
	}

	public String getSharedPreferences(String key) {
		mShareRefs = getSharedPreferences(STRING_NAME_USERSETTING, Activity.MODE_PRIVATE);
		return mShareRefs.getString(key, "");
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_PROGRESS);
		setContentView(R.layout.login_screen);

		String pass = getSharedPreferences("password");
		String username = getSharedPreferences("username");
		((EditText) findViewById(R.id.txtUsername)).setText(username);
		((EditText) findViewById(R.id.txtPassword)).setText(pass);
	}

	public void ForgotAccount(View v) {
		new RestoreAccountDialog(this).show();
	}

	
	
	
	public void CreateAccountButtonClick(View v) {
		new CreateAccountDialog(this).show();
	}

	public void LoginButtonClick(View v) {
		final ProgressDialog pd = Dialog.showProgressDialog(this,"Login", "Please wait...", false);
		final Thread threadLogin = new Thread() {
			public void run() {
				try {

					final String username = ((EditText) findViewById(R.id.txtUsername)).getText().toString();
					final String password = ((EditText) findViewById(R.id.txtPassword)).getText().toString();
					if (username.equals("") || password.equals("")) {
						myHandler.post(new Runnable() {
							@Override
							public void run() {
								pd.dismiss();
								Toast.makeText(AccountManagerScreen.this, "Please enter your account info", Toast.LENGTH_LONG);
							}
						});
					} else {
						final String msg = WSF.checkLogin(username, password);
						myHandler.post(new Runnable() {
							
							public void run() {
								Toast.makeText(AccountManagerScreen.this, msg, Toast.LENGTH_LONG);
							}
						});
						
						USERID = Integer.parseInt(msg.trim());
						if (USERID == -1) {
							myHandler.post(new Runnable() {
								@Override
								public void run() {
									pd.dismiss();
									Toast.makeText(AccountManagerScreen.this, "Wrong username or password", Toast.LENGTH_LONG);
								}
							});
						} else {
							US = username;
							PW = password;
							final boolean chk = ((CheckBox) AccountManagerScreen.this.findViewById(R.id.CheckBox01)).isChecked();
							myHandler.post(new Runnable() {
								@Override
								public void run() {
									if (chk) {
										AccountManagerScreen.this.addSharedPreferences("password", password);
										AccountManagerScreen.this.addSharedPreferences("username", username);
									} else {
										AccountManagerScreen.this.addSharedPreferences("password", "");
										AccountManagerScreen.this.addSharedPreferences("username", "");
									}
									Intent intent = new Intent(AccountManagerScreen.this, ContactsScreen.class);
									startActivity(intent);
									pd.dismiss();
								}
							});
						}
					}

				} catch (final Exception e) {
					myHandler.post(new Runnable() {
						@Override
						public void run() {
							pd.dismiss();
							e.printStackTrace();
							Dialog.show(AccountManagerScreen.this, "Error!", e.getMessage() + "\nPlease try again", null, null, null, null, Type.ERROR);
						}						
					});
					
				}
			}
		};
		threadLogin.start();
	}
}